package com.hymjweb.demo.hymj.basic_data_manage.xt_company.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_company.bean.XtCompanyCachePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_company.bean.XtCompanyPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.hymj.basic_data_manage.xt_company.mapper
 * @Author: luoge
 * @CreateTime: 2020-10-11 21:11
 * @Description:
 */

@Mapper
@Component
public interface XtCompanyMapper {

    class XtCompanyProvider{

        public String findCacheAll() {
            return new SQL() {{
                SELECT("xc.id,xc.name,xc.phone,xc.registered_address,xc.legal_person,xc.master_bill_foreign_company_id");
                FROM("xt_company xc");
            }}.toString();
        }


        public String findAll(Map map) {
            String sql= new SQL() {{
                SELECT("xc.id,xc.name,xc.phone,xc.registered_address,xc.legal_person,xc.business_scope,xc.master_bill_foreign_company_id");
                FROM("xt_company xc where 1 = 1 ");
            }}.toString();

            if (map != null) {
                String name = DataTypeUtil.toNotNullString(map.get("keyName"));
                String phone = DataTypeUtil.toNotNullString(map.get("keyPhone"));
                String registeredAddress = DataTypeUtil.toNotNullString(map.get("keyRegisteredAddress"));
                String legalPerson = DataTypeUtil.toNotNullString(map.get("keyLegalPerson"));

                if(!DataTypeUtil.isEmptyStr(name)){
                    sql += " and xc.name like " + DataTypeUtil.quoteAllLikeStr(name);
                }
                if(!DataTypeUtil.isEmptyStr(phone)){
                    sql += " and xc.phone like " + DataTypeUtil.quoteAllLikeStr(phone);
                }
                if(!DataTypeUtil.isEmptyStr(registeredAddress)){
                    sql += " and xc.registered_address like " + DataTypeUtil.quoteAllLikeStr(registeredAddress);
                }
                if(!DataTypeUtil.isEmptyStr(legalPerson)){
                    sql += " and xc.legal_person like " + DataTypeUtil.quoteAllLikeStr(legalPerson);
                }
            }
            return  sql;
        }

        public String findObjectById(String id) {
            return new SQL() {{
                SELECT("xc.id,xc.name,xc.phone,xc.registered_address,xc.legal_person,xc.business_scope,xc.master_bill_foreign_company_id");
                FROM("xt_company xc");
                WHERE("xc.id = " + DataTypeUtil.quoteStr(id));
            }}.toString();
        }


        public String insert(XtCompanyPojo obj) {
            String sql= new SQL() {{
                INSERT_INTO("xt_company");
                VALUES("id","#{id}");
                VALUES("name","#{name}");
                VALUES("phone","#{phone}");
                VALUES("registered_address","#{registeredAddress}");
                VALUES("legal_person","#{legalPerson}");
                VALUES("business_scope","#{businessScope}");
                VALUES("master_bill_foreign_company_id","#{masterBillForeignCompanyId}");
            }}.toString();
            return sql;
        }

        public String update(XtCompanyPojo obj) {
            String sql= new SQL() {{
                UPDATE("xt_company xc");
                SET("xc.name = #{name}");
                SET("xc.phone = #{phone}");
                SET("xc.registered_address = #{registeredAddress}");
                SET("xc.legal_person = #{legalPerson}");
                SET("xc.business_scope = #{businessScope}");
                SET("xc.master_bill_foreign_company_id = #{masterBillForeignCompanyId}");
                WHERE("xc.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("xt_company");
                WHERE("id = " + id );
            }}.toString();
            return sql;
        }

    }


    @Results(id="companyCacheMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "phone", property = "phone"),
            @Result(column = "registered_address", property = "registeredAddress"),
            @Result(column = "legal_person", property = "legalPerson"),
            @Result(column = "master_bill_foreign_company_id", property = "masterBillForeignCompanyId")
    })
    @SelectProvider(type = XtCompanyProvider.class, method = "findCacheAll")
    List<XtCompanyCachePojo> findCacheAll();



    @Results(id="companyMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "phone", property = "phone"),
            @Result(column = "registered_address", property = "registeredAddress"),
            @Result(column = "legal_person", property = "legalPerson"),
            @Result(column = "business_scope", property = "businessScope"),
            @Result(column = "master_bill_foreign_company_id", property = "masterBillForeignCompanyId")
    })
    @SelectProvider(type = XtCompanyProvider.class, method = "findAll")
    List<XtCompanyPojo> findAll(Map map);


    @ResultMap(value = "companyMapId")
    @SelectProvider(type = XtCompanyProvider.class, method = "findObjectById")
    XtCompanyPojo findObjectById(@Param("id") String id);


    @InsertProvider(type = XtCompanyProvider.class, method = "insert")
    @Options(keyProperty = "id")
    int insert(XtCompanyPojo obj);


    @UpdateProvider(type = XtCompanyProvider.class, method = "update")
    int update(XtCompanyPojo obj);

    @DeleteProvider(type = XtCompanyProvider.class, method = "delete")
    int delete(String id);

}
